package controllers

import play.api.libs.json.Json
import play.api.mvc._
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by yz on 2018/7/25
  */
class DownloadController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

  def toIndex = Action { implicit request =>
    Ok(views.html.download.index())
  }

  def downloadData = Action { implicit request =>
    val data = FormTool.fileNameForm.bindFromRequest().get
    val file = new File(Tool.downloadDir, data.fileName)
    Ok.sendFile(file).as("application/x-download").
      withHeaders(
        CACHE_CONTROL -> "max-age=3600",
        CONTENT_DISPOSITION -> ("attachment; filename=" + file.getName)
      )
  }

}
